package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentRepository travelCommentRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelComment.setCreateTime(new Date());

        //维护关联评论
        String refId = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(refId)){
            TravelComment refcomment = this.get(refId);
            travelComment.setRefComment(refcomment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }

        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {

        TravelComment travelComment = travelCommentRepository.findById(id).orElse(null);
        return travelComment;
    }

    @Override
    public List<TravelComment> listAll() {
        return travelCommentRepository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        //创建查询对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }




        PageRequest pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        Page<TravelComment> page = DBHelper.query(template, TravelComment.class, query, qo.getPageable());
        return page;
    }

    @Override
    public List<TravelComment> queryByTravelId(String travelId) {

        return travelCommentRepository.findByTravelId(travelId);
    }


}
